package com.zdlyh.controller;

import com.zdlyh.constants.SystemConstants;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.AddCommentDto;
import com.zdlyh.domain.entity.Comment;
import com.zdlyh.enums.AppHttpCodeEnum;
import com.zdlyh.exception.SystemException;
import com.zdlyh.service.CommentService;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/comment")
@Api(tags = "评论",description = "评论相关接口")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/commentList")
    @ApiOperation(value = "查询文章评论列表",notes = "查询文章评论列表")
    public ResponseResult  getCommentList(Long articleId,Integer pageNum,Integer pageSize){
        return commentService.getCommentList(SystemConstants.Comment_Type_Arictle,articleId,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation(value = "新增评论",notes = "用户发布评论")
    public ResponseResult  addComment(@RequestBody AddCommentDto addCommentDto){
        Comment comment = BeanCopyUtils.copyBean(addCommentDto, Comment.class);
        // 需要登录后才能评论
        if (Objects.isNull(SecurityUtils.getLoginUser())){
            throw new SystemException(AppHttpCodeEnum.NEED_LOGIN);
        }
        return commentService.addComment(comment);
    }

    @GetMapping("/linkCommentList")
    @ApiOperation(value = "查询友链评论列表",notes = "查询友链评论列表")
    public ResponseResult  getLinkCommentList(Integer pageNum,Integer pageSize){
        return commentService.getCommentList(SystemConstants.Comment_Type_Link, null,pageNum,pageSize);
    }
}
